using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class UserControls_Admin_EditProductDepartmentAndCategory : System.Web.UI.UserControl
{
    string departmentId;
    string categoryId;
    string categoryName;

    protected void Page_Load(object sender, EventArgs e)
    {
        
        ProductDetails currentProduct = CatalogAccess.GetProductDetails(Request.QueryString["ProductID"]);
        if (!IsPostBack)
        {
            pNameTextBox.Text = currentProduct.Name;
            pPriceTextBox.Text = currentProduct.Price.ToString("0");
            catalogPromotionCheckBox.Checked = currentProduct.OnCatalogPromotion;
            departmentPromotionCheckBox.Checked = currentProduct.OnDepartmentPromotion;
            // lista z producentami
            brandDropDownList.DataSource = CatalogAccess.GetBrands();
            brandDropDownList.DataBind();
            //kk brandDropDownList.SelectedItem.Text = currentProduct.BrandName;
            //kk brandDropDownList.SelectedItem.Text = currentProduct.BrandName;
            brandDropDownList.SelectedIndex = brandDropDownList.Items.IndexOf(brandDropDownList.Items.FindByValue(currentProduct.BrandID));
            
            departmentDropDownList.DataSource = CatalogAccess.GetDepartments();
            departmentDropDownList.DataBind();

            //departmentDropDownList.Items. Insert(0, " - Wybierz kategorie - ");
            //departmentDropDownList.SelectedIndex = Int32.Parse(currentProduct.DepartmentID);
            departmentDropDownList.SelectedIndex = departmentDropDownList.Items.IndexOf(departmentDropDownList.Items.FindByValue(currentProduct.DepartmentID));

            //kk departmentDropDownList.SelectedItem.Value = currentProduct.DepartmentID;
            //kk departmentDropDownList.SelectedItem.Text = currentProduct.DepartmentName;
            

            
            categoryDropDownList.DataSource = CatalogAccess.GetCategoriesInDepartment(departmentDropDownList.SelectedItem.Value);
            categoryDropDownList.DataBind();

            
            //kk categoryDropDownList.SelectedItem.Text = currentProduct.CategoryName;
            //kk categoryDropDownList.SelectedItem.Value = currentProduct.CategoryID; 
            categoryDropDownList.SelectedIndex = categoryDropDownList.Items.IndexOf(categoryDropDownList.Items.FindByValue(currentProduct.CategoryID));
            
            onBasketCheckBox.Checked = currentProduct.Basket;

            //brandDropDownList.Items.Insert(0, " - Wybierz Producenta - ");

        }
        departmentId = currentProduct.DepartmentID;
        categoryId = currentProduct.CategoryID;
        categoryName = currentProduct.CategoryName;

        statusLabel.ForeColor = System.Drawing.Color.Red;


        
        
       // //categoryDropDownList.SelectedItem.Text = currentProduct.CategoryName;
       //// categoryDropDownList.SelectedItem.Value = currentProduct.CategoryID; 
       // if (departmentDropDownList.SelectedItem.Value == currentProduct.DepartmentID)
       // {

       // }
       // else 
       // {
       //     categoryDropDownList.Items.Clear();
       //     categoryDropDownList.DataSource = CatalogAccess.GetCategoriesInDepartment(departmentDropDownList.SelectedItem.Value);
       //     categoryDropDownList.DataBind();
       // }
       // if (categoryDropDownList.SelectedItem.Value == currentProduct.CategoryID)
       // {
          
       // }
       // else 

       // {
          
       //     categoryDropDownList.SelectedItem.Value = categoryId;
       //     categoryDropDownList.SelectedItem.Text = categoryName;
       // }
 



        
    }

    protected void goBackLink_Click(object sender, EventArgs e)
    {
        departmentId = departmentDropDownList.SelectedItem.Value;
        categoryId = categoryDropDownList.SelectedItem.Value;

        Response.Redirect(Request.ApplicationPath + "/Admin/ProductsAdmin.aspx?DepartmentID=" + departmentId
           + "&CategoryID=" + categoryId);

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = pNameTextBox.Text.ToString();
        string des = CatalogAccess.GetProductDetails(Request.QueryString["ProductID"]).Description;
        string price = pPriceTextBox.Text.ToString();
        string image1 = CatalogAccess.GetProductDetails(Request.QueryString["ProductID"]).Image1FileName;
        string image2 = CatalogAccess.GetProductDetails(Request.QueryString["ProductID"]).Image2FileName;
        string catP = catalogPromotionCheckBox.Checked.ToString();
        string depP = departmentPromotionCheckBox.Checked.ToString();
        string brandid = brandDropDownList.SelectedItem.Value;
        string categoryidd = categoryDropDownList.SelectedItem.Value;
        string bask = onBasketCheckBox.Checked.ToString();


        string productid = Request.QueryString["ProductID"];
        bool success = CatalogAccess.UpdateProductWithDepartmentAndCategory(productid, name, des, price, image1, image2, catP, depP, brandid, bask, categoryidd);

        statusLabel.Text = success ? "Aktualizacja udana" : "Aktualizacja nieudana";


    }
    protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ProductDetails currentProduct = CatalogAccess.GetProductDetails(Request.QueryString["ProductID"]);
        categoryDropDownList.DataSource = CatalogAccess.GetCategoriesInDepartment(departmentDropDownList.SelectedItem.Value);
        categoryDropDownList.DataBind();
        categoryDropDownList.SelectedIndex = categoryDropDownList.Items.IndexOf(categoryDropDownList.Items.FindByValue(currentProduct.CategoryID));
    }
}

